/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JDialog;

import org.opu.pc.services.PublisherService;
import org.opu.pc.services.beans.PublisherBean;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.client.widgets.PropertiesEditPane.PropertyOption;

/**
 * 
 * @author yura
 */
public class NewPublisher extends AbstractDialogAction {

	private static final long serialVersionUID = -4802608673019243436L;

	protected PublisherService publisherService;

	public void setPublisherService(PublisherService publisherService) {
		this.publisherService = publisherService;
	}

	@Override
	public String getName() {
		return "NewPublisher";
	}

	@Override
	protected void initPropMap(Map<String, List<PropertyOption>> map) {
		map.put("isStudent", Arrays.asList(
				new PropertyOption("applied.true", "true", "true"), 
				new PropertyOption("applied.false", "false", "false")));
	}

	@Override
	protected void initProp(LinkedHashMap<String, Object> properties) {
		properties.put("name", "");
		properties.put("secondName", "");
		properties.put("lastName", "");
		properties.put("isStudent", "true");
	}

	@Override
	protected OkAction getOkAction(JDialog dialog, final PropertiesEditPane pane) {
		return new OkAction(pane, dialog) {
			private static final long serialVersionUID = 1L;

			@Override
			public void runAction(ActionEvent e) throws Exception {
                hideDialog();
				Map<String, Object> prop = pane.getProperties();
				String name = (String)prop.get("name");
				String surname = (String)prop.get("secondName");
				String fatherName = (String)prop.get("lastName");
				String isStudentStr = (String)prop.get("isStudent");
				boolean isStudent = Boolean.parseBoolean(isStudentStr);
				okAction(name, surname, fatherName, isStudent);
			}
		};
	}

	protected void okAction(String name, String secondName, String lastName,
			boolean isStudent) {
		mainView.setContent(Arrays.asList(publisherService.createNew(name,
				secondName, lastName, isStudent)), PublisherBean.class);
	}

	@Override
	protected String getCacelActionName() {
		return "Close";
	}

}
